package com.emep.changzhi.analyse.service.aqi;

import com.emep.changzhi.analyse.entity.aqi.AreaAqiMoniter;
import com.emep.changzhi.analyse.entity.aqi.DataAqiInfo;
import com.emep.changzhi.analyse.entity.basics.SysAqiInfo;
import com.emep.changzhi.analyse.repository.aqi.AqiCityRepository;
import com.emep.changzhi.analyse.repository.aqi.AqiRealTimeRepository;
import com.emep.changzhi.analyse.repository.basics.SysAqiInfoRepository;
import com.emep.changzhi.analyse.utils.DateTimeUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.util.*;

/**
 * @author xy
 * @Description: 实时空气质量
 * @date 2018/3/28 9:37
 * @version V1.0
 */

@Slf4j
@Data
@Service
@Transactional
public class AqiRealTimeService{
    @Resource
    AqiRealTimeRepository aqiRealTimeRepository;
    @Resource
    AqiCityRepository aqiCityRepository;
    @Resource
    SysAqiInfoRepository sysAqiInfoRepository;

    /**
     * @author xy
     * @Description: 获取实时空气质量柱状图
     * @date 2018/3/29 9:36
     * @version V1.0
     */
    public Map<String,Object> list(String area_id){
        Map<String,Object> map = new HashMap<>();
        String date_now = DateTimeUtil.getDateTimeForString(new Date(),DateTimeUtil.DATE_DEFAULT_FORMAT)+" "+ DateTimeUtil.getNowHour()+":00:00";
        Date date = null;
        try {
            date = DateTimeUtil.stringToDate(date_now,DateTimeUtil.DATETIME_DEFAULT_FORMAT);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<AreaAqiMoniter> list =aqiCityRepository.findByAreaIdAndUpdate1(area_id,date.getTime()+"");
        for(int i=0;i<=list.size();i++){
            map.put("AQI",list.size()==0 ? "-" : list.get(i).getAqi().toString());
            map.put("cityName",list.size()==0 ? "-" : list.get(i).getCity().toString());
            map.put("timestamp",list.size()==0 ? "-" : list.get(i).getUpdate1().toString());
            map.put("Co",list.size()==0 ? "-" : list.get(i).getCo().toString());
            map.put("No2",list.size()==0 ? "-" : list.get(i).getNo2().toString());
            map.put("Pm10",list.size()==0 ? "-" : list.get(i).getPm10().toString());
            map.put("Pm25",list.size()==0 ? "-" : list.get(i).getPm25().toString());
            map.put("O3",list.size()==0 ? "-" : list.get(i).getO31h().toString());
            map.put("So2",list.size()==0 ? "-" : list.get(i).getSo2().toString());
            map.put("primary_pollutant",list.size()==0 ? "-" : list.get(i).getPrimaryPollutant().toString());
            break;
        }
        List<AreaAqiMoniter> list_1h = aqiCityRepository.findByAreaIdAndUpdate1(area_id,DateTimeUtil.mathHourOfDate(date,-1).getTime()+"");
        for(int i=0;i<=list_1h.size();i++){
            map.put("Co_1h",list_1h.size()==0 ? "-" : list_1h.get(i).getCo().toString());
            map.put("No2_1h",list_1h.size()==0 ? "-" : list_1h.get(i).getNo2().toString());
            map.put("Pm10_1h",list_1h.size()==0 ? "-" : list_1h.get(i).getPm10().toString());
            map.put("Pm25_1h",list_1h.size()==0 ? "-" : list_1h.get(i).getPm25().toString());
            map.put("O3_1h",list_1h.size()==0 ? "-" : list_1h.get(i).getO31h().toString());
            map.put("So2_1h",list_1h.size()==0 ? "-" : list_1h.get(i).getSo2().toString());
            break;
        }
        List<AreaAqiMoniter> list_2h = aqiCityRepository.findByAreaIdAndUpdate1(area_id,DateTimeUtil.mathHourOfDate(date,-2).getTime()+"");
        for(int i=0;i<=list_2h.size();i++){
            map.put("Co_2h",list_2h.size()==0 ? "-" : list_2h.get(i).getCo().toString());
            map.put("No2_2h",list_2h.size()==0 ? "-" : list_2h.get(i).getNo2().toString());
            map.put("Pm10_2h",list_2h.size()==0 ? "-" : list_2h.get(i).getPm10().toString());
            map.put("Pm25_2h",list_2h.size()==0 ? "-" : list_2h.get(i).getPm25().toString());
            map.put("O3_2h",list_2h.size()==0 ? "-" : list_2h.get(i).getO31h().toString());
            map.put("So2_2h",list_2h.size()==0 ? "-" : list_2h.get(i).getSo2().toString());
            break;
        }
        List<AreaAqiMoniter> list_3h = aqiCityRepository.findByAreaIdAndUpdate1(area_id,DateTimeUtil.mathHourOfDate(date,-3).getTime()+"");
        for(int i=0;i<=list_3h.size();i++){
            map.put("Co_3h",list_3h.size()==0 ? "-" : list_3h.get(i).getCo().toString());
            map.put("No2_3h",list_3h.size()==0 ? "-" : list_3h.get(i).getNo2().toString());
            map.put("Pm10_3h",list_3h.size()==0 ? "-" : list_3h.get(i).getPm10().toString());
            map.put("Pm25_3h",list_3h.size()==0 ? "-" : list_3h.get(i).getPm25().toString());
            map.put("O3_3h",list_3h.size()==0 ? "-" : list_3h.get(i).getO31h().toString());
            map.put("So2_3h",list_3h.size()==0 ? "-" : list_3h.get(i).getSo2().toString());
            break;
        }
        List<SysAqiInfo> list_standard = sysAqiInfoRepository.findByIsDelete("0");
        List<Integer> nums = new ArrayList<Integer>();
        for(int i=0;i<list_standard.size();i++){
            nums.add(Integer.parseInt(list_standard.get(i).getAqi()));
        }
        Collections.sort(nums);
        int num=0;
        for(int i=0;i<nums.size();i++){
            if(list.size()==0){
                map.put("Co_standard", "-");
                map.put("No2_standard","-");
                map.put("Pm10_standard","-");
                map.put("Pm25_standard","-");
                map.put("O3_standard","-");
                map.put("So2_standard","-");
                break;
            }
            if (Integer.parseInt(list.get(0).getAqi())<=0){
                num= nums.get(i);
            }else if(Integer.parseInt(list.get(0).getAqi())>=nums.get(nums.size()-1)){
                num= nums.get(nums.size()-1);
            }else if(Integer.parseInt(list.get(0).getAqi())>nums.get(i) && Integer.parseInt(list.get(0).getAqi())<nums.get(i+1)){
                num= nums.get(i);
                for(int j=0;j<=list_standard.size();j++){
                    if(num==Integer.parseInt(list_standard.get(j).getAqi())){
                        map.put("Co_standard",list_standard.size()==0 ? "-" : list_standard.get(j).getCo());
                        map.put("No2_standard",list_standard.size()==0 ? "-" : list_standard.get(j).getNo2());
                        map.put("Pm10_standard",list_standard.size()==0 ? "-" : list_standard.get(j).getO3());
                        map.put("Pm25_standard",list_standard.size()==0 ? "-" : list_standard.get(j).getSo2());
                        map.put("O3_standard",list_standard.size()==0 ? "-" : list_standard.get(j).getPm10_24());
                        map.put("So2_standard",list_standard.size()==0 ? "-" : list_standard.get(j).getPm25_24());
                        break;
                    }
                }
                break;
            }
        }
        return map;

    }
    public List<AreaAqiMoniter> realtime(String areaCode){
        List<AreaAqiMoniter> list = aqiCityRepository.findOneByAreaId(areaCode);
        return list;
    }


}
